﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YJKRevitFamilyDataBase;

namespace YJKFamilyLibManager.FamilyLibModule
{
	public partial class DlgAddCategory : System.Windows.Forms.Form
	{
		public DlgAddCategory(DlgMain main)
		{
			this.mForm = main;
			this.InitializeComponent();
			if (this.mForm.trCategory.SelectedNode != null && this.mForm.trCategory.SelectedNode.Tag != null)
			{
				this.cbxSubject.Items.Add(this.mForm.CurrentSubject);
				this.cbxSubject.SelectedIndex = 0;
				this.cbxCategory.Items.Add(this.mForm.trCategory.SelectedNode.Tag);
				this.cbxCategory.SelectedIndex = 0;
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			if (this.cbxCategory.SelectedItem is Parent)
			{
				try
				{
					Parent parent = (Parent)this.cbxCategory.SelectedItem;
					DbCategory dbCategory = new DbCategory(this.tbxCName.Text);
					using (List<DbCategory>.Enumerator enumerator = parent.GetAllChildren(false).GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							if (enumerator.Current.Name == dbCategory.Name)
							{
								MessageBox.Show("分类名重复，请更换分类名再添加。");
								return;
							}
						}
					}
					dbCategory.Version = this.tbxVersion.Text;
					dbCategory.Description = this.rtbDescription.Text;
					dbCategory.Image = new Bitmap(200, 200);
					for (int i = 0; i < this.lbxRecordParam.Items.Count; i++)
					{
						dbCategory.ParamDefinitionList.Add((DbParamDefinition)this.lbxRecordParam.Items[i]);
					}
					if (parent.AddCategory(dbCategory))
					{
						TreeNode treeNode = new TreeNode(dbCategory.Name);
						treeNode.ImageIndex = 10;
						treeNode.Tag = dbCategory;
						treeNode.Name = dbCategory.Name;
						this.mForm.trCategory.SelectedNode.Nodes.Add(treeNode);
						this.tbxCName.Text = "";
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message);
				}
			}
			if (!this.chkContinue.Checked)
			{
				base.Close();
			}
		}

		public static DlgAddCategory GetInstance(DlgMain main)
		{
			if (DlgAddCategory.dac == null || DlgAddCategory.dac.IsDisposed)
			{
				DlgAddCategory.dac = new DlgAddCategory(main);
				DlgAddCategory.dac.MdiParent = main.ActiveMdiChild;
			}
			return DlgAddCategory.dac;
		}

		private void tbxCName_TextChanged(object sender, EventArgs e)
		{
			Regex regex = new Regex("^[0-9a-zA-Z一-龥]+$");
			if (this.tbxCName.Text.Trim() == "" || !regex.IsMatch(this.tbxCName.Text.Trim()))
			{
				this.btnOK.Enabled = false;
				return;
			}
			this.btnOK.Enabled = true;
		}

		private void cbxSubject_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (this.tbxParamName != null)
			{
                DbParamDefinition dbParamDefinition = new DbParamDefinition(this.tbxParamName.Text, 0, ParamDefinitionType.CaregoryParam, (Autodesk.Revit.DB.BuiltInParameterGroup)(-1));
				if (!this.lbxRecordParam.Items.Contains(dbParamDefinition))
				{
					this.lbxRecordParam.Items.Add(dbParamDefinition);
					this.tbxParamName.Text = "";
					return;
				}
				MessageBox.Show("参数重名，请重新输入！", "族库管理");
				this.tbxParamName.Text = "";
			}
		}

		private void tbxParamName_TextChanged(object sender, EventArgs e)
		{
			if (new Regex("^\\w").Match(this.tbxParamName.Text).Success)
			{
				this.btnAdd.Enabled = true;
				return;
			}
			this.btnAdd.Enabled = false;
		}

		private void lbxRecordParam_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem != null)
			{
				this.btnDel.Enabled = true;
				this.propertyGrid1.SelectedObject = (DbParamDefinition)this.lbxRecordParam.SelectedItem;
				return;
			}
			this.btnDel.Enabled = false;
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem != null)
			{
				int selectedIndex = this.lbxRecordParam.SelectedIndex;
				this.lbxRecordParam.Items.RemoveAt(selectedIndex);
			}
		}

		private void DlgAddCategory_Load(object sender, EventArgs e)
		{
			this.lbxRecordParam.DoubleClick += this.lbxRecordParam_DoubleClick;
		}

		private void lbxRecordParam_DoubleClick(object sender, EventArgs e)
		{
			if (this.lbxRecordParam.SelectedItem != null)
			{
				int selectedIndex = this.lbxRecordParam.SelectedIndex;
				this.lbxRecordParam.Items.RemoveAt(selectedIndex);
			}
		}

		private void DlgAddCategory_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private static DlgAddCategory dac;

		private DlgMain mForm;
	}
}
